<?php

 /**
  * @package JooS
  * @subpackage Syntax
  */

  require_once "JooS/Syntax/Abstract.php";

  abstract class JooS_Syntax_Complex extends JooS_Syntax_Abstract {
    /**
     * @var JooS_Syntax_Abstract
     */
    protected $_rule = null;

    /**
     * @return JooS_Syntax_Abstract
     */
    protected function createRule() {
      return null;
    }

    /**
     *
     * @param int $num
     * @return JooS_Syntax_Abstract
     */
    public function getRule($num = null) {
      return is_null($num) ? $this->_rule : $this->_rule[$num];
    }

    /**
     * @param int $num
     * @return string
     */
    public function getParsedValue($num = null) {
      $parsedValue = false;
      if (is_null($num)) {
        $parsedValue = $this->_parsedValue;
      }
      else {
        $Rule = $this->getRule($num);
        if ($Rule)
          $parsedValue = $Rule->getParsedValue();
      }
      return $parsedValue;
    }

    public function init() {
      if (func_num_args()) {
        $rule = func_get_arg(0);
        if ($rule)
          $this->_rule = $rule;
      }
    }

    /**
     * @return string
     */
    public function compileSyntax() {
      if (is_null($this->_rule))
        $this->_rule = $this->createRule();

      $exprPos = $this->getParser()->getPos();
      $compiled = parent::compileSyntax();
      if ($compiled === false)
        $this->getParser()->setPos($exprPos);

      return $compiled;
    }
  }
